package com.zyong.design.pattern.flyweight.case2;

import java.util.Random;

/**
 * @program: zyong-design-pattern
 * @ClassName: Client
 * @author: zyong
 * @create: 2024-04-06 11:11
 */
public class Client {
    public static void main(String[] args) {
        int times = 10;

        // 不使用享元模式
        long startTime = System.currentTimeMillis();

        for (int i = 0; i < times; i++) {
            GameFactory.getTankWithOutFlyWeight(randomTankType());
        }

        long endTime = System.currentTimeMillis();
        System.out.printf("不使用享元模式的速度: %d 毫秒.\n", (endTime - startTime));

        // 使用享元模式
        init();
        startTime = System.currentTimeMillis();

        for (int i = 0; i < times; i++) {
            GameFactory.getTank(randomTankType());
        }

        endTime = System.currentTimeMillis();
        System.out.printf("使用享元模式的速度: %d 毫秒.\n", (endTime - startTime));

    }

    public static void init() {
        String[] colors = {"黄色", "银色", "灰色"};
        String[] speeds = {"10M/s", "5M/s", "1M/s"};

        for (String color : colors) {
            for (String speed : speeds) {
                TankType tankType = new TankType(color, speed);

                GameFactory.getTank(tankType);
            }
        }

        GameFactory.getHome("红队");
        GameFactory.getHome("蓝队");
    }

    public static TankType randomTankType() {
        String[] colors = {"黄色", "银色", "灰色"};
        String[] speeds = {"10M/s", "5M/s", "1M/s"};

        Random random = new Random();
        int randomColor = random.nextInt(3);
        String color = colors[randomColor];

        int randomSpeed = random.nextInt(3);
        String speed = speeds[randomSpeed];

        return new TankType(color, speed);
    }
}
